dojo.provide("lotex.widget.Report");

dojo.require("dojo.dom");
dojo.require("dojo.event");
dojo.require("dojo.html.*");
dojo.require("dojo.lang.common");
dojo.require("dojo.io.*");
dojo.require("dojo.string");
dojo.require("dojo.html.style");
dojo.require("dojo.widget.HtmlWidget");

dojo.widget.tags.addParseTreeHandler("dojo:Report");
dojo.widget.defineWidget(
    "lotex.widget.Report",
    dojo.widget.HtmlWidget,
    {
        templatePath:null,
        templateCssPath:null,
        postCreate:function(){
            //     summary
            //    overridden from HtmlWidget, initializes and renders the widget.
            // var thead=dojo.html.getElementsByClass("dojo:for",this.domNode)[0];
            // this.domNode.getElementsByClassName("for1");
            // dojo.debugShallow(thead);
            var list = new lotex.system.Render(this.domNode, this.dataUri);
            list.renderComplete = dojo.lang.hitch(this, "process");
            // list.renderComplete = this.process;
            list.render();
            this.data = list.data;
            //dojo.event.connect(list, "renderComplete", this, "process");

            //
            //alert(9);
            //dojo.event.connect(list, "renderComplete", this, function(){alert("hahah");});
        },

        process: function() {
            this.table = this.domNode.getElementsByTagName("table")[0];
            //var d = this.table.rows[2].cells[2].innerHTML;
            //dojo.debugShallow(this.table.rows);
            this.maxRowIdx = this.table.rows.length;
            this.maxColIdx = this.table.cells.length / this.maxRowIdx;
            dojo.debug("this.maxRowIdx "+this.maxRowIdx+" this.maxColIdx "+this.maxColIdx);

            for(var i = 0; i < this.table.cells.length; i++){
                var cell = this.table.cells[i];
                var curRow = Math.floor(i/this.maxColIdx);
                var curCol = cell.cellIndex;
                dojo.debug("curRow "+curRow+" curCol "+curCol);
                //dojo.debug(cell.innerText);
                var str = cell.innerText;
                if(dojo.string.startsWith(str,"=")) {
                    cell.innerText = this.parse(str,curRow,curCol);
                    //dojo.debugShallow(cell);
                    //cell.cellIndex;
                }
            }
        },

        parse: function(str,curRow,curCol) {
            // remove "="
            var cmd = str.replace(/=/g,'');
            dojo.debug("Parse1: "+cmd);
            cmd = cmd.replace(/([A-Z])/g,"#"+"$1");
            dojo.debug("Parse2: "+cmd);
            // B[n] parse:
            // this.getCell(currentRow,2);
            cmd = cmd.replace
                (/#([A-Z])\[n\]/g,
                 function($0,$1,$2) {
                    var col = $1.charCodeAt(0)-65;
                    return "this.getCell("+curRow+","+col+")";
                });
            // B parse:
            // this.getCell(null,2);
            cmd = cmd.replace
                (/#([A-Z])/g,
                 function($0,$1,$2) {
                    var col = $1.charCodeAt(0)-65;
                    return "this.getCell(null,"+col+")";
                });
            // sum parse:
            // this.sum
            cmd = cmd.replace
                (/(sum|percent)/g,
                 function($0,$1,$2) {
                    return "this." + $1;
                });
            dojo.debug("Parse: "+cmd);
            return eval(cmd);
        },

        getCell: function(row,col) {
            if(row==null) {
                var ret = [];
                for(var i=0;i<this.maxRowIdx;i++)
                    ret.push(this.getCell(i,col));
                return ret;
            }
            return this.table.rows[row].cells[col].innerText;
        },

        sum: function(a) {
            var count=0;
            for(var i=0;i<a.length;i++){
                var value = isNaN(parseInt(a[i])) ? 0 : parseInt(a[i]);
                //dojo.debug("a["+i+"]: "+value + " isNum: "+dojo.lang.isNumber(value));
                count += value;
            }
            return count;
        },

        percent: function(a) {
            return a*100 + "%";
        }
});